//
//  ZFCustomControlView.m
//  ZFPlayer_Example
//
//  Created by 紫枫 on 2019/6/5.
//  Copyright © 2019 紫枫. All rights reserved.
//

#import "ZFCustomControlView.h"
#import "ZFUtilities.h"
#import <ZFPlayer/ZFPlayerController.h>
#import <ZFPlayer/ZFPlayerConst.h>
#import "ZFSliderView.h"

@interface ZFCustomControlView () <ZFSliderViewDelegate>
@property (nonatomic, assign) BOOL isShow;

@property (nonatomic, assign) BOOL controlViewAppeared;

@property (nonatomic, strong) dispatch_block_t afterBlock;

@property (nonatomic, assign) NSTimeInterval sumTime;
/// 加载loading
//@property (nonatomic, strong) ZFSpeedLoadingView *activity;
/// 封面图
@property (nonatomic, strong) UIImageView *coverImageView;


@property (nonatomic,strong) UIImageView *pauseImageView;


@property (nonatomic,strong)UIActivityIndicatorView *spin;


@end

@implementation ZFCustomControlView
@synthesize player = _player;

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       
//        [self addSubview:self.activity];
        [self addSubview:self.pauseImageView];
        self.autoFadeTimeInterval = 0.2;
        self.autoHiddenTimeInterval = 2.5;
        self.controlViewAppeared = YES;
    

        self.spin = [[UIActivityIndicatorView alloc] init];
        self.spin.color = [UIColor whiteColor];
        self.spin.backgroundColor = C(0, 0.7);
        self.spin.layer.cornerRadius = 5;
        self.spin.layer.masksToBounds = YES;
        self.spin.hidesWhenStopped = YES;
        
        [self addSubview:self.spin];
        
        
        [self resetControlView];
        
        
        
        self.clipsToBounds = YES;
    }
    return self;
}

#pragma mark - action



- (void)fullScreenButtonClickAction:(UIButton *)sender {
    [self.player enterFullScreen:!self.player.isFullScreen animated:YES];
}

#pragma mark - 添加子控件约束

- (void)layoutSubviews {
    [super layoutSubviews];
//
//    CGFloat min_x = 0;
//    CGFloat min_y = 0;
//    CGFloat min_w = 0;
//    CGFloat min_h = 0;
//
    
    self.coverImageView.frame = self.bounds;
    
//    min_w = 80;
//    min_h = 80;
//    self.activity.frame = CGRectMake(min_x, min_y, min_w, min_h);
//    self.activity.cl_centerX = self.cl_centerX;
//    self.activity.cl_centerY = self.cl_centerY + 10;
    
   
    self.spin.frame = (CGRect){0,0,60,60};
    self.spin.center = CGPointMake(kScreenW / 2.0f, kScreenH / 2.5);
    
    self.pauseImageView.frame = CGRectMake(0, 0, 70, 80);
    self.pauseImageView.cl_centerX = self.cl_centerX;
    self.pauseImageView.cl_centerY = self.cl_centerY;
    
    
    
}

/**
 设置标题、封面、全屏模式
 
 @param coverUrl 视频的封面，占位图默认是灰色的
 @param fullScreenMode 全屏模式
 */
- (void)coverURLString:(NSString *)coverUrl fullScreenMode:(ZFFullScreenMode)fullScreenMode appeared:(BOOL)appeared{
   
    [self resetControlView];
    [self layoutIfNeeded];
    [self setNeedsDisplay];
    
    self.controlViewAppeared = appeared;
    
    self.player.orientationObserver.fullScreenMode = fullScreenMode;
    [self.player.currentPlayerManager.view.coverImageView sd_setImageWithURL:[NSURL URLWithString:coverUrl] placeholderImage:nil];
//    [self.player.currentPlayerManager.view.coverImageView setImageWithURLString:coverUrl placeholder:placeholder];
}

#pragma mark - private

/** 重置ControlView */
- (void)resetControlView {
    self.backgroundColor             = [UIColor clearColor];
}


#pragma mark - ZFPlayerControlViewDelegate

/// 手势筛选，返回NO不响应该手势
- (BOOL)gestureTriggerCondition:(ZFPlayerGestureControl *)gestureControl gestureType:(ZFPlayerGestureType)gestureType gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer touch:(nonnull UITouch *)touch {
    if (self.player.isSmallFloatViewShow && !self.player.isFullScreen && gestureType != ZFPlayerGestureTypeSingleTap) {
        return NO;
    }
    return YES;
}

/// 单击手势事件
- (void)gestureSingleTapped:(ZFPlayerGestureControl *)gestureControl {
//    if (!self.player) return;
//    if (self.player.isSmallFloatViewShow && !self.player.isFullScreen) {
//        [self.player enterFullScreen:YES animated:YES];
//    } else {
//        self.controlViewAppeared = !self.controlViewAppeared;
//        if (self.controlViewAppearedCallback) {
//            self.controlViewAppearedCallback(self.controlViewAppeared);
//        }
//    }

    BOOL isPlaying = NO;
    
    if (self.player.currentPlayerManager.playState == ZFPlayerPlayStatePlaying) {
        [self.player.currentPlayerManager pause];
    }else if(self.player.currentPlayerManager.playState == ZFPlayerPlayStatePaused){
        [self.player.currentPlayerManager play];
        isPlaying = YES;
    }
    if (self.controlViewPlayerStatusCallback) {
        self.controlViewPlayerStatusCallback(isPlaying);
    }
}
/// 双击手势事件
- (void)gestureDoubleTapped:(ZFPlayerGestureControl *)gestureControl {
//    [self playOrPause];
    

}

/// 捏合手势事件，这里改变了视频的填充模式
- (void)gesturePinched:(ZFPlayerGestureControl *)gestureControl scale:(float)scale {
    if (scale > 1) {
        self.player.currentPlayerManager.scalingMode = ZFPlayerScalingModeAspectFill;
    } else {
        self.player.currentPlayerManager.scalingMode = ZFPlayerScalingModeAspectFit;
    }
}

/// 准备播放
- (void)videoPlayer:(ZFPlayerController *)videoPlayer prepareToPlay:(NSURL *)assetURL {
    
}

/// 播放状态改变
- (void)videoPlayer:(ZFPlayerController *)videoPlayer playStateChanged:(ZFPlayerPlaybackState)state {
   
    if (state == ZFPlayerPlayStatePlaying) {
        /// 开始播放时候判断是否显示loading
        if (videoPlayer.currentPlayerManager.loadState == ZFPlayerLoadStateStalled) {
            if (!self.spin.isAnimating) {
                [self.spin startAnimating];
            }
        } else if ((videoPlayer.currentPlayerManager.loadState == ZFPlayerLoadStateStalled || videoPlayer.currentPlayerManager.loadState == ZFPlayerLoadStatePrepare)) {
            if (!self.spin.isAnimating) {
                [self.spin startAnimating];
            }
        }else{
            if (self.spin.isAnimating) {
                [self.spin stopAnimating];
            }
        }
        self.pauseImageView.hidden = YES;
    } else if (state == ZFPlayerPlayStatePaused) {
        self.pauseImageView.hidden = NO;
        /// 暂停的时候隐藏loading
        if (self.spin.isAnimating) {
            [self.spin stopAnimating];
        }
    } else if (state == ZFPlayerPlayStatePlayFailed) {
        if (self.spin.isAnimating) {
            [self.spin stopAnimating];
        }
    }
}

/// 加载状态改变
- (void)videoPlayer:(ZFPlayerController *)videoPlayer loadStateChanged:(ZFPlayerLoadState)state {
    if (state == ZFPlayerLoadStatePrepare) {
        self.coverImageView.hidden = NO;
    } else if (state == ZFPlayerLoadStatePlaythroughOK || state == ZFPlayerLoadStatePlayable) {
        self.coverImageView.hidden = YES;
        self.player.currentPlayerManager.view.backgroundColor = [UIColor blackColor];
    }
    if (state == ZFPlayerLoadStateStalled && videoPlayer.currentPlayerManager.isPlaying) {
        if (!self.spin.isAnimating) {
            [self.spin startAnimating];
        }
    } else if ((state == ZFPlayerLoadStateStalled || state == ZFPlayerLoadStatePrepare) && videoPlayer.currentPlayerManager.isPlaying) {
        if (!self.spin.isAnimating) {
            [self.spin startAnimating];
        }
    } else {
        if (self.spin.isAnimating) {
            [self.spin stopAnimating];
        }
    }
}



- (void)videoPlayer:(ZFPlayerController *)videoPlayer presentationSizeChanged:(CGSize)size {
    
}

/// 视频view即将旋转
- (void)videoPlayer:(ZFPlayerController *)videoPlayer orientationWillChange:(ZFOrientationObserver *)observer {
    
}

/// 视频view已经旋转
- (void)videoPlayer:(ZFPlayerController *)videoPlayer orientationDidChanged:(ZFOrientationObserver *)observer {
    
    [self layoutIfNeeded];
    [self setNeedsDisplay];
}

/// 锁定旋转方向
- (void)lockedVideoPlayer:(ZFPlayerController *)videoPlayer lockedScreen:(BOOL)locked {
    
}

#pragma mark - setter

- (void)setPlayer:(ZFPlayerController *)player {
    _player = player;
}

#pragma mark - getter

- (UIImageView *)coverImageView {
    if (!_coverImageView) {
        _coverImageView = [[UIImageView alloc] init];
        _coverImageView.userInteractionEnabled = YES;
        _coverImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _coverImageView;
}

- (UIImageView *)pauseImageView{
    if (!_pauseImageView) {
        _pauseImageView = [[UIImageView alloc]init];
        _pauseImageView.image = [UIImage imageNamed:@"home_videoStop_tag"];
        _pauseImageView.contentMode = UIViewContentModeScaleAspectFit;
        _pauseImageView.hidden = YES;
    }
    return _pauseImageView;
}

@end
